package ar.com.mindset.geometrics.colorSchemes {
	import ar.com.mindset.geometrics.ColorEntry;
	import ar.com.mindset.geometrics.ColorScheme;
	import ar.com.mindset.geometrics.MapItem;
	import ar.com.mindset.geometrics.RegionData;
	import ar.com.mindset.geometrics.RegionsLegend;
	import ar.com.mindset.geometrics.colorSchemes.continuous.DivisionStrategy;
	import ar.com.mindset.geometrics.colorSchemes.continuous.LinealDivisionStrategy;
	import ar.com.mindset.geometrics.legends.BarLegend;
	
	import com.degrafa.GeometryGroup;
	import com.degrafa.paint.SolidFill;
	
	import mx.collections.ArrayCollection;

	[DefaultProperty("divisionStrategy")]

	public class ContinuousColorScheme extends ColorScheme {

		public override function getLegend():GeometryGroup{
			return legend as GeometryGroup;
		}

		private var divisionValues:ArrayCollection;
		public var legend:RegionsLegend = new BarLegend(350,400,350) ;
		public var name:String="Continuous values";
		public var divisionStrategy:DivisionStrategy = new LinealDivisionStrategy();

		public override function prepare(mapItem:MapItem, regionData:RegionData):void {
			var solidFill:SolidFill=new SolidFill()

			solidFill.color=getColor(regionData[valueProperty]);

			mapItem.fillcolor=solidFill;
		}

		private function getColor(value:Object):Object {
			for each (var colorEntry:ColorEntry in divisionValues) {
				if (value < colorEntry.key) {
					return colorEntry.color;
				}
			}
			return divisionStrategy.defaultColor;
		}

		public function toString():String {
			return name;
		}

		public override function initialize(regionDataCollection:ArrayCollection):void {
			this.divisionValues = divisionStrategy.getDivisions(this, regionDataCollection, valueProperty);
			legend.initialize(divisionValues, divisionStrategy.defaultColor);
		}
	}
}